/**
 * 
 */
package com.itump.base.util;

import java.net.URL;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>标题: JAVA标准化管理系统</p>
 * <p>作者：杨勇</p>
 * <p>功能描述: 内网发送邮件</p>
 * <p>创建时间：2011-07-17 19：25 </p>
 * <p>版本信息：1.0 </p>
 * <p>Copyright: Copyright (c) 2008</p>
 
 */
public class SendEmailUtil {
	protected static final Log logger = LogFactory.getLog(SendEmailUtil.class);

	String smtpHost;// = "mail01";

	String userName; //= "pmiczyf";

	String password;// = "gnpjvc";

	String charset = "GBK";

	String adSuffix;// = "@gr.com";

	//String adSuffix="@gnpjvc.com.cn";
	String webserver="";

	private static SendEmailUtil emailBean;	
	
    private PropertiesReader preader = PropertiesReader.getIntance();
    private String jumpserver = preader.getProperty("jumpserver");
    private String urldtms = preader.getProperty("urldtms");

	/**
	 * 获取发送email实例
	 * 
	 * @return Email
	 */

	public static void main(String[] args) {
		SendEmailUtil tmsEmail = SendEmailUtil.getInstance();
		tmsEmail.sendMail("yangyong", "yangyong", "中文", "yangyong test 中文", "", "");

	}

	public static SendEmailUtil getInstance() {
		if (emailBean == null) {
			PropertiesReader preader = PropertiesReader.getIntance();

			String smtpHost = preader.getProperty("smtp.hostname", "mail01");
			String userName = preader.getProperty("smtp.username", "pcitliy");
			String password = preader.getProperty("smtp.password", "greatroad");
			String adSuffix = preader.getProperty("smtp.suffix","@gnpjvc.com.cn");			
			String webserver = preader.getProperty("web.server", "http://10.1.16.205:7005/tm");
			String weblink = preader.getProperty("jumpserver");
			webserver = "<a href='"+weblink+"' target='_blank'>"+webserver+"</a>";

			logger.debug("smtpHost:==============" + smtpHost);
			logger.debug("userName:==============" + userName);
			logger.debug("password:==============" + password);
			logger.debug("adSuffix:==============" + adSuffix);

			emailBean = new SendEmailUtil(smtpHost, userName, password, adSuffix,webserver);
		}
		return emailBean;
	}

	/**
	 * 构造函数
	 * 
	 * @param smtpHost
	 *            String
	 * @param userName
	 *            String
	 * @param password
	 *            String
	 */
	private SendEmailUtil(String smtpHost, String userName, String password,
			String adSuffix,String webserver) {
		this.smtpHost = smtpHost;
		this.userName = userName;
		this.password = password;
		this.adSuffix = adSuffix;
		this.webserver = webserver;
	}

	/**
	 * 发送email
	 * 
	 * @param from
	 *            发信人登陆用户名 String
	 * @param to
	 *            收信人登录用户名 多个用户名，中间用 ; 号 隔开 String
	 * @param subject
	 *            主题，发信的标题 String
	 * @param message
	 *            消息主体 发信的主题内容 String
	 * @return boolean
	 */

	public boolean sendMailattach(String to, String subject, String message,String attachname,String gid) {
		return sendMail(userName, to, null, null, subject, message, null, attachname,gid);
	}
	
	/**
	 * 发送email
	 * 
	 * @param from
	 *            发信人登陆用户名 String
	 * @param to
	 *            收信人登录用户名 多个用户名，中间用 ; 号 隔开 String
	 * @param subject
	 *            主题，发信的标题 String
	 * @param message
	 *            消息主体 发信的主题内容 String
	 * @return boolean
	 */

	public boolean sendMail(String to,String subject, String message,String gid) {
		return sendMail(userName, to, null, null, subject, message, "", "",gid);
	}
	
	/**
	 * 发送email
	 * 
	 * @param from
	 *            发信人登陆用户名 String
	 * @param to
	 *            收信人登录用户名 多个用户名，中间用 ; 号 隔开 String
	 * @param cc 抄送人 多个用户名，中间用 ; 号 隔开 String           
	 * @param subject
	 *            主题，发信的标题 String
	 * @param message
	 *            消息主体 发信的主题内容 String
	 * @return boolean
	 */

	public boolean sendCCMail(String to,String cc, String subject, String message,String gid) {
		return sendMail(userName, to, cc, null, subject, message, "", "",gid);
	}

	/**
	 * 发送email
	 * 
	 * @param from
	 *            发信人登陆用户名 String
	 * @param to
	 *            收信人登录用户名 多个用户名，中间用 ; 号 隔开 String
	 * @param subject
	 *            主题，发信的标题 String
	 * @param message
	 *            消息主体 发信的主题内容 String
	 * @return boolean
	 */

	public boolean sendMailfrom(String from, String to, String subject,
			String message,String gid) {
		return sendMail(from, to, null, null, subject, message, "", "",gid);
	}

	/**
	 * 发送email
	 * 
	 * @param from
	 *            发信人 String
	 * @param to
	 *            收信人 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 * @param url
	 *            附件路径 String
	 * @param attachName
	 *            附件名称 String
	 * @return boolean
	 */

	public boolean sendMail(String from, String to, String subject,
			String message, String url, String attachName,String gid) {
		return sendMail(from, to, null, null, subject, message, url, attachName,gid);
	}
	
	/**
	 * 发送email
	 * 
	 * @param to
	 *            收信人 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 * @param url
	 *            附件路径 String
	 * @param attachName
	 *            附件名称 String
	 * @return boolean
	 */
	
	public boolean sendMail( String to, String subject,
			String message, String url, String attachName,String gid) {
		return sendMail( userName,to, null, null, subject, message, url, attachName,gid);
	}

	/**
	 * 发送email
	 * 
	
	 * @param to
	 *            收信人 String
	 * @param cc
	 *            抄送人 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 * @param url
	 *            附件路径 String
	 * @param attachName
	 *            附件名称 String
	 * @return boolean
	 */
	public boolean sendMaill(String to, String cc, String subject,
			String message, String url, String attachName,String gid) {
		return sendMail(userName, to, cc, null, subject, message, url, attachName,gid);
	}
	
	/**
	 * 发送email
	 * 
	 * @param from
	 *            发信人 String
	 * @param to
	 *            收信人 String
	 * @param cc
	 *            抄送人 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 * @param url
	 *            附件路径 String
	 * @param attachName
	 *            附件名称 String
	 * @return boolean
	 */
	public boolean sendMail(String from, String to, String cc, String subject,
			String message, String url, String attachName,String gid) {
		return sendMail(from, to, cc, null, subject, message, url, attachName,gid);
	}
	/**
	 * 发送email
	 * 
	 
	 * @param to
	 *            收信人 String
	 * @param cc
	 *            抄送人 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 
	 * @return boolean
	 */
	public boolean sendMail(String to,String cc,String subject,String message,String gid){
		return sendMail(userName,to,cc,null,subject,message,"","",gid);
	}
	/**
	 * 群发 发送email
	 * 
	 * @param from
	 *            发信人 String
	 * @param passWord
	 *            发信人密码 String
	 * @param to
	 *            收信人 String
	 * @param cc
	 *            抄送人 String
	 * @param bcc
	 *            暗抄 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 * @param url
	 *            附件路径 String
	 * @param attachName
	 *            附件名称 String
	 * @return boolean
	 */

	public boolean sendMail(String from,String passWord, String to, String cc, String bcc,
			String subject, String message, String url, String attachName,String gid) {
		try {
			
			
			Properties props = new Properties();
			props.put("mail.smtp.host", smtpHost);
			props.put("mail.smtp.auth", "true");
			EmailAutherticatorbean ea = new EmailAutherticatorbean(from,
					passWord);

			Session session = Session.getInstance(props, (Authenticator) ea);
			
			MimeMessage mimemessage = new MimeMessage(session);

			//发信人
			//from = userName;
			if (from != null)
				mimemessage.setFrom(new InternetAddress(from + adSuffix));

			//收信人
			InternetAddress toAddress[] = this.getAddress(to);
			if (to != null) {
				mimemessage.setRecipients(RecipientType.TO, toAddress);

			}

			//抄送人
			InternetAddress ccAddress[] = this.getAddress(cc);
			if (cc != null)
				mimemessage.setRecipients(RecipientType.CC, ccAddress);

			//暗抄人
			InternetAddress bccAddress[] = this.getAddress(bcc);
			if (bcc != null)
				mimemessage.setRecipients(RecipientType.BCC, bccAddress);			
			
			mimemessage.setSubject(subject,"GBK");
			MimeMultipart mimemultipart = new MimeMultipart();

			MimeBodyPart mimebodypart = new MimeBodyPart();
			
			String link = "<br>--请访问链接--<br>";		
/*****************运营公司修改链接地址********************************************************************/
			if(gid == null || gid.equals("null"))gid="";
			if(gid.startsWith("31")){          
				link += "<a href='"+jumpserver+"' target='_blank'>"+urldtms+"</a>";
			}else{
				link += webserver;
			}			
/*******************************************************************************************************/
			
			if(message.indexOf("${LINK}") != -1 ){
			 message=message.replaceAll("\\$\\{LINK\\}", link);
			}else{
				message += link; 
			}
		
			mimebodypart.setContent(message, "text/html; charset=\"" + charset
					+ "\"");
			//mimebodypart.setDataHandler(new DataHandler(message,"text/html;charset=gb2312"));
			mimemultipart.addBodyPart(mimebodypart);
			
		
			if (!"".equals(url) && !"".equals(attachName)) {
				MimeBodyPart mimebodypart1 = new MimeBodyPart();

				
				URL urlPath = Thread.currentThread().getContextClassLoader().getResource("tmpmailfile");
				String path = urlPath.getPath();
			
				FileDataSource fds=new FileDataSource(path + "/" + attachName);
				mimebodypart1.setDataHandler(new DataHandler(fds));				
				mimebodypart1.setFileName(fds.getName());
				
				mimemultipart.addBodyPart(mimebodypart1);
			}

			mimemessage.setContent(mimemultipart);

			Transport transport = session.getTransport("smtp");
		
			transport.connect();
			Transport.send(mimemessage);
			transport.close();

			return true;

		} 
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 群发 发送email
	 * 
	 * @param from
	 *            发信人 String
	 * @param to
	 *            收信人 String
	 * @param cc
	 *            抄送人 String
	 * @param bcc
	 *            暗抄 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 * @param url
	 *            附件路径 String
	 * @param attachName
	 *            附件名称 String
	 * @return boolean
	 */

	public boolean sendMail(String from, String to, String cc, String bcc,
			String subject, String message, String url, String attachName,String gid) {
		try {
			
			
			Properties props = new Properties();
			props.put("mail.smtp.host", smtpHost);
			props.put("mail.smtp.auth", "true");
			EmailAutherticatorbean ea = new EmailAutherticatorbean(userName,
					password);

			Session session = Session.getInstance(props, (Authenticator) ea);
			
			MimeMessage mimemessage = new MimeMessage(session);

			//发信人
			from = userName;
			if (from != null)
				mimemessage.setFrom(new InternetAddress(from + adSuffix));

			//收信人
			InternetAddress toAddress[] = this.getAddress(to);
			if (to != null) {
				mimemessage.setRecipients(RecipientType.TO, toAddress);

			}

			//抄送人
			InternetAddress ccAddress[] = this.getAddress(cc);
			if (cc != null)
				mimemessage.setRecipients(RecipientType.CC, ccAddress);

			//暗抄人
			InternetAddress bccAddress[] = this.getAddress(bcc);
			if (bcc != null)
				mimemessage.setRecipients(RecipientType.BCC, bccAddress);			
			
			mimemessage.setSubject(subject,"GBK");
			MimeMultipart mimemultipart = new MimeMultipart();

			MimeBodyPart mimebodypart = new MimeBodyPart();
			
			String link = "<br>--请访问链接--<br>";		
/*****************运营公司修改链接地址********************************************************************/
			if(gid == null || gid.equals("null"))gid="";
			if(gid.startsWith("31")){          
				link += "<a href='"+jumpserver+"' target='_blank'>"+urldtms+"</a>";
			}else{
				link += webserver;
			}			
/*******************************************************************************************************/
			
			if(message.indexOf("${LINK}") != -1 ){
			 message=message.replaceAll("\\$\\{LINK\\}", link);
			}else{
				message += link; 
			}
		
			mimebodypart.setContent(message, "text/html; charset=\"" + charset
					+ "\"");
			//mimebodypart.setDataHandler(new DataHandler(message,"text/html;charset=gb2312"));
			mimemultipart.addBodyPart(mimebodypart);
			
		
			if (!"".equals(url) && !"".equals(attachName)) {
				MimeBodyPart mimebodypart1 = new MimeBodyPart();

				
				URL urlPath = Thread.currentThread().getContextClassLoader().getResource("tmpmailfile");
				String path = urlPath.getPath();
			
				FileDataSource fds=new FileDataSource(path + "/" + attachName);
				mimebodypart1.setDataHandler(new DataHandler(fds));				
				mimebodypart1.setFileName(fds.getName());
				
				mimemultipart.addBodyPart(mimebodypart1);
			}

			mimemessage.setContent(mimemultipart);

			Transport transport = session.getTransport("smtp");
		
			transport.connect();
			Transport.send(mimemessage);
			transport.close();

			return true;

		} 
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	public boolean sendOutMail(String to, String subject, String message,String gid) {
		return sendOutMail(null, to, null, null, subject, message, null, null,gid);
	}
	
	
	public boolean sendOutMail(String to, String subject, String message,String attachname,String gid) {
		return sendOutMail(null, to, null, null, subject, message, null, attachname,gid);
	}
	
	public boolean sendOutMail(String from, String to, String cc, String bcc,
			String subject, String message, String url, String attachName,String gid) {
		try {

			Properties props = new Properties();
			props.put("mail.smtp.host", smtpHost);
			props.put("mail.smtp.auth", "true");
			EmailAutherticatorbean ea = new EmailAutherticatorbean(userName,
					password);

			Session session = Session.getInstance(props, (Authenticator) ea);
		
			MimeMessage mimemessage = new MimeMessage(session);

			//发信人
			from = userName;
			if (from != null)
				mimemessage.setFrom(new InternetAddress(from + adSuffix));

			//收信人
			
			if (to != null) {
				mimemessage.setRecipients(RecipientType.TO, to);
			}

			//抄送人
			
			if (cc != null)
				mimemessage.setRecipients(RecipientType.CC, cc);

			//暗抄人			
			if (bcc != null)
				mimemessage.setRecipients(RecipientType.BCC, bcc);

			mimemessage.setSubject(subject,"GBK");
			MimeMultipart mimemultipart = new MimeMultipart();

			MimeBodyPart mimebodypart = new MimeBodyPart();
			
			message +="<br>--请访问链接--<br>";		
/*****************运营公司修改链接地址********************************************************************/
			if(gid == null || gid.equals("null"))gid="";
			if(gid.startsWith("31")){          
				message += "<a href='"+jumpserver+"' target='_blank'>"+urldtms+"</a>";
			}else{
				message += webserver;
			}			
/*******************************************************************************************************/
			
			
			mimebodypart.setContent(message, "text/html; charset=\"" + charset
					+ "\"");
			//mimebodypart.setDataHandler(new DataHandler(message,"text/html;charset=gb2312"));
			mimemultipart.addBodyPart(mimebodypart);
			
			if (!"".equals(url) && !"".equals(attachName)) {
				MimeBodyPart mimebodypart1 = new MimeBodyPart();
				
				URL urlPath = Thread.currentThread().getContextClassLoader().getResource("tmpmailfile");
				String path = urlPath.getPath();
				FileDataSource fds=new FileDataSource(path + "/" + attachName);
				mimebodypart1.setDataHandler(new DataHandler(fds));
				mimebodypart1.setFileName(fds.getName());
				
				mimemultipart.addBodyPart(mimebodypart1);
			}

			mimemessage.setContent(mimemultipart);

			Transport transport = session.getTransport("smtp");
			//transport.connect((String)props.get("mail.smtp.host"),userName,password);
			transport.connect();
			Transport.send(mimemessage);
			transport.close();

			return true;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 单发 发送email
	 * 
	 * @param from
	 *            发信人 String
	 * @param to
	 *            收信人 String
	 * @param cc
	 *            抄送人 String
	 * @param bcc
	 *            暗抄 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 * @param url
	 *            附件路径 String
	 * @param attachName
	 *            附件名称 String
	 * @return boolean
	 */

	public boolean sendMailSings(String from, String to, String subject,
			String message, String url, String attachName,String gid) {
		return sendMailSings(from, to, null, null, subject, message, url,
				attachName,gid);
	}

	public boolean sendMailSings(String from, String to, String cc,
			String subject, String message, String url, String attachName,String gid) {
		return sendMailSings(from, to, cc, null, subject, message, url,
				attachName,gid);
	}

	public boolean sendMailSings(String from, String to, String cc, String bcc,
			String subject, String message, String url, String attachName,String gid) {
		try {
			InternetAddress toAddress[] = this.getAddress(to);
			if (toAddress != null && toAddress.length > 0) {
				for (int i = 0; i < toAddress.length; i++) {
					sendMailSing(from, toAddress[i], cc, bcc, subject, message,
							url, attachName,gid);
				}
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public void sendMailSing(String from, InternetAddress to, String cc,
			String bcc, String subject, String message, String url,
			String attachName,String gid) throws Exception {
		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", smtpHost);
			props.put("mail.smtp.auth", "true");
			EmailAutherticatorbean ea = new EmailAutherticatorbean(userName,
					password);

			Session session = Session.getInstance(props, (Authenticator) ea);
			MimeMessage mimemessage = new MimeMessage(session);
			
			

			//发信人
			from = userName;
			if (from != null)
				mimemessage.setFrom(new InternetAddress(from + adSuffix));

			//收信人			
			if (to != null)
				mimemessage.setRecipient(RecipientType.TO, to);

			//抄送人
			InternetAddress ccAddress[] = this.getAddress(cc);
			if (cc != null)
				mimemessage.setRecipients(RecipientType.TO, ccAddress);

			//暗抄人
			InternetAddress bccAddress[] = this.getAddress(bcc);
			if (bcc != null)
				mimemessage.setRecipients(RecipientType.TO, bccAddress);
		
			mimemessage.setSubject(subject,"GBK");
			MimeMultipart mimemultipart = new MimeMultipart();

			MimeBodyPart mimebodypart = new MimeBodyPart();
			
			message +="<br>--请访问链接--<br>";		
/*****************运营公司修改链接地址********************************************************************/
			if(gid == null || gid.equals("null"))gid="";
			if(gid.startsWith("31")){          
				message += "<a href='"+jumpserver+"'  target='_blank'>"+urldtms+"</a>";
			}else{
				message += webserver;
			}			
/*******************************************************************************************************/
						
			mimebodypart.setContent(message, "text/html; charset=\"" + charset
					+ "\"");
			//mimebodypart.setDataHandler(new DataHandler(message,"text/html;charset=gb2312"));
			mimemultipart.addBodyPart(mimebodypart);

			
			if (!"".equals(url) && !"".equals(attachName)) {
				MimeBodyPart mimebodypart1 = new MimeBodyPart();
				
				URL urlPath = Thread.currentThread().getContextClassLoader().getResource("tmpmailfile");
				String path = urlPath.getPath();
				FileDataSource fds=new FileDataSource(path + "/" + attachName);
				mimebodypart1.setDataHandler(new DataHandler(fds));
				mimebodypart1.setFileName(fds.getName());
				
				mimemultipart.addBodyPart(mimebodypart1);
			}

			mimemessage.setContent(mimemultipart);

			Transport transport = session.getTransport("smtp");
			transport.connect();
			Transport.send(mimemessage);
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private InternetAddress[] getAddress(String address)
			throws AddressException {
	try {
			String[] addresss = Utils.strToArr(address, ";");
			if (addresss == null || addresss.length < 1)
				return null;
			else {
				InternetAddress[] ia = new InternetAddress[addresss.length];
				for (int i = 0; i < addresss.length; i++) {

					ia[i] = new InternetAddress(addresss[i] + adSuffix);
				}
				return ia;
			}
		} catch (Exception e) {
			return null;
		}
	}

	public String replaceUser(String content, String username) {
		username = username + ":";
		return replaceStr(content, "$users", username);
	}

	public String replaceAllUser(String content, String usernames) {
		usernames = usernames + ":";
		return replaceStr(content, "$users", usernames);
	}

	public String replaceAllUser(String content) {
		return replaceStr(content, "$users", "领导,同事们:");
	}

	public String replaceContent(String content, String approver,
			String applyer, String operator) {
		if (approver == null || approver.equals(""))
			approver = "领导";
		if (applyer == null || applyer.equals(""))
			applyer = "大家好";
		if (operator == null || operator.equals(""))
			operator = "大家好";
		content = replaceStr(content, "${APPROVER}", approver);
		content = replaceStr(content, "${USER}", approver);
		content = replaceStr(content, "${APPLYER}", applyer);
		content = replaceStr(content, "${OPERATOR}", operator);
		content = replaceStr(content, "${DATE}", (new Date()).toString());
		content = replaceStr(content, "${CONTENT}", content);
		return content;
	}

	public String replaceTitle(String content, String applyer, String url) {
		if (applyer == null || applyer.equals(""))
			applyer = "员工";
		if (url == null || url.equals(""))
			url = "";
		content = replaceStr(content, "${APPLYER}", applyer);
		content = replaceStr(content, "${USER}", applyer);
		content = replaceStr(content, "${HREF}", url);
		content = replaceStr(content, "${DATE}", (new Date()).toString());
		content = replaceStr(content, "${CONTENT}", content);
		return content;
	}

	public String replaceStr(String src, String oldStr, String newStr) {
		int index = src.indexOf(oldStr);

		if (index == -1)
			return src;

		int len = src.length();
		int sublen = oldStr.length();

		return (src.substring(0, index) + newStr + replaceStr(src.substring(
				index + sublen, len), oldStr, newStr));
	}
	/**
	 * 群发 发送email
	 * 
	 * @param from
	 *            发信人 String
	 * @param to
	 *            收信人 String
	 * @param cc
	 *            抄送人 String
	 * @param bcc
	 *            暗抄 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 * @param url
	 *            附件路径 String
	 * @param attachName
	 *            附件名称 String
	 * @return boolean
	 */

	public boolean sendMail1(String from, String to, String cc, String bcc,
			String subject, String message, String url, String attachName) {
		try {
			
			
			Properties props = new Properties();
			props.put("mail.smtp.host", smtpHost);
			props.put("mail.smtp.auth", "true");
			EmailAutherticatorbean ea = new EmailAutherticatorbean(userName,
					password);

			Session session = Session.getInstance(props, (Authenticator) ea);
			
			MimeMessage mimemessage = new MimeMessage(session);

			//发信人
			from = userName;
			if (from != null)
				mimemessage.setFrom(new InternetAddress(from + adSuffix));

			//收信人
			InternetAddress toAddress[] = this.getAddress(to);
			if (to != null) {
				mimemessage.setRecipients(RecipientType.TO, toAddress);

			}

			//抄送人
			InternetAddress ccAddress[] = this.getAddress(cc);
			if (cc != null)
				mimemessage.setRecipients(RecipientType.CC, ccAddress);

			//暗抄人
			InternetAddress bccAddress[] = this.getAddress(bcc);
			if (bcc != null)
				mimemessage.setRecipients(RecipientType.BCC, bccAddress);			
			
			mimemessage.setSubject(subject,"GBK");

			MimeMultipart mimemultipart = new MimeMultipart();

			MimeBodyPart mimebodypart = new MimeBodyPart();
			
		
			mimebodypart.setContent(message, "text/html; charset=\"" + charset
					+ "\"");
			//mimebodypart.setDataHandler(new DataHandler(message,"text/html;charset=gb2312"));
			mimemultipart.addBodyPart(mimebodypart);
			

		
			if (!"".equals(url) && !"".equals(attachName)) {
				MimeBodyPart mimebodypart1 = new MimeBodyPart();
				//FileDataSource fds=new FileDataSource(url);
				URLDataSource fds=new URLDataSource(new URL(url)); 
				mimebodypart1.setDataHandler(new DataHandler(fds));
			    mimebodypart1.setFileName(fds.getName());
			   // mimebodypart1.setFileName(new String(attachName.getBytes(), "ISO8859-1"));
			   
				
				mimemultipart.addBodyPart(mimebodypart1);
			}

			mimemessage.setContent(mimemultipart);

			Transport transport = session.getTransport("smtp");
		
			transport.connect();
			Transport.send(mimemessage);
			transport.close();

			return true;

		} 
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 功能：采用当前用户的邮件帐号来发送email
	 * 
	 * @param from
	 *            发信人 String
	 * @param outlookpass
	 *            当前用户outlook邮件密码 String
	 * @param to
	 *            收信人 String
	 * @param cc
	 *            抄送人 String
	 * @param bcc
	 *            暗抄 String
	 * @param subject
	 *            主题 String
	 * @param message
	 *            消息主体 String
	 * @param url
	 *            附件路径 String
	 * @param attachName
	 *            附件名称 String
	 * @param gid
	 *            公司ID String
	 * @return boolean
	 * Yang Yong 2008.08.04
	 */

	public boolean sendMailCurr(String from, String outlookpass,String to, String cc, String bcc,
			String subject, String message, String url, String attachName,String gid) {
		try {
			
			
			Properties props = new Properties();
			props.put("mail.smtp.host", smtpHost);
			props.put("mail.smtp.auth", "true");
			EmailAutherticatorbean ea = new EmailAutherticatorbean(from,
					outlookpass);

			Session session = Session.getInstance(props, (Authenticator) ea);
			
			MimeMessage mimemessage = new MimeMessage(session);

			//发信人
			if (from != null)
				mimemessage.setFrom(new InternetAddress(from + adSuffix));

			//收信人
			InternetAddress toAddress[] = this.getAddress(to);
			if (to != null) {
				mimemessage.setRecipients(RecipientType.TO, toAddress);

			}

			//抄送人
			InternetAddress ccAddress[] = this.getAddress(cc);
			if (cc != null)
				mimemessage.setRecipients(RecipientType.CC, ccAddress);

			//暗抄人
			InternetAddress bccAddress[] = this.getAddress(bcc);
			if (bcc != null)
				mimemessage.setRecipients(RecipientType.BCC, bccAddress);			
			
			mimemessage.setSubject(subject,"GBK");

			MimeMultipart mimemultipart = new MimeMultipart();

			MimeBodyPart mimebodypart = new MimeBodyPart();
			
			mimebodypart.setContent(message, "text/html; charset=\"" + charset+ "\"");
			mimemultipart.addBodyPart(mimebodypart);

			if (!"".equals(url) && !"".equals(attachName)) {
				MimeBodyPart mimebodypart1 = new MimeBodyPart();
				URLDataSource fds=new URLDataSource(new URL(url)); 
				mimebodypart1.setDataHandler(new DataHandler(fds));
			    mimebodypart1.setFileName(fds.getName());
				mimemultipart.addBodyPart(mimebodypart1);
			}

			mimemessage.setContent(mimemultipart);

			Transport transport = session.getTransport("smtp");
		
			transport.connect();
			Transport.send(mimemessage);
			transport.close();

			return true;

		} 
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 
	 * <p>
	 * Title: 发送smtp验证
	 * </p>
	 * <p>
	 * Description: 发送邮件验证
	 * </p>
	 * <p>
	 * Copyright: Copyright (c) 2004
	 * </p>
	 * <p>
	 * Company: entel
	 * </p>
	 * 
	 * @author zzj
	 * @version 1.0
	 */
	private class EmailAutherticatorbean extends Authenticator {
		private String m_username = null;

		private String m_userpass = null;

		public void setUsername(String username) {
			m_username = username;
		}

		public void setUserpass(String userpass) {
			m_userpass = userpass;
		}

		public EmailAutherticatorbean(String username, String userpass) {
			super();
			setUsername(username);
			setUserpass(userpass);
		}

		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(m_username, m_userpass);
		}

	}
}

